-- =============================================================================
-- 
--       Filename:  SecBid.lua
-- 
--    Description:  Allows you to hold secret dkp auctions
-- 
--        Version:  0.0.4
--        Created:  Thu Mar 26 08:49:50 CET 2009
--       Revision:  none
-- 
--         Author:  Mathias Jost (mathias.jost@stud.fh-swf.de)
-- 
-- =============================================================================

-- creates a new addon object and mixes in the librarys
SecBid = LibStub("AceAddon-3.0"):NewAddon("SecBid", "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0")

-- creates an object with the options
local options = {
    name = "SecBid",
    handler = SecBid,
    type = 'group',
    args = {
			time = {
					type = "input",
					name = "Auction Time",
					desc = "Sets the time in seconds the auction is supposed to run for.",
					usage = "<time in seconds>",
					set = "SetTimeout",
					get = false,
					pattern = "%d+",
			},
			pickup = {
					type = "input",
					name = "Pickup start amount",
					desc = "Sets the the amount of dkp for players not in the database.",
					usage = "<number>",
					set = "SetNewPlayerDkp",
					get = false,
					pattern = "%d+",
			},
			start = {
					type = "input",
					name = "Start the auction",
					desc = "Starts the auction for a given startingbid and item.",
					usage = "<## [itemlink]>",
					set = "StartAuction",
					get = false,
					guiHidden = true,
					validate = 	function(a, args)
												-- gets the starting amount and itemlink
												local start, item = SecBid:GetArgs(args, 2)

												-- checks if the starting amount or itemlink are invalid
												if (tonumber(start) == nil) or (item == nil) then
													return "usage: /secbid start ## [itemlink]"
												end

												return true
											end,

			},
    },
}

-- addon variables
local BiddingActive = false
local HighBidder = nil
local warning = 0
local timer = nil

-- default options
local defaults = {
    global = {
			time = 15,
			NewPlayerDkp = 60,
    },
}


-- Will be called when the addon is first loaded
function SecBid:OnInitialize()
	-- Debug msg at Addon Load 
  SecBid:Print("SecBid Test Load")
	
	-- Register the AceDB for saving in the saved variables
	SecBid.db = LibStub("AceDB-3.0"):New("SecBidDB", defaults, "Default")
	
	-- register an options table for the options menu
	LibStub("AceConfig-3.0"):RegisterOptionsTable("SecBid", options, {"secbid", "sb"})
		
	-- Register Events
	SecBid:RegisterEvent("CHAT_MSG_WHISPER")
end

-- handles the fireing of the CHAT_MSG_WHISPER event
function SecBid:CHAT_MSG_WHISPER(eventname, message, sender)
	-- check for the "dkp" message witch returns the updated points to the player
	if message == "dkp" then
		local PlayerFound = false
		
		-- check if the player has current dkp data saved
		for name in pairs(gdkp.players) do
			-- checks if the sender is in the current dkp database
			if name == sender then
				-- returns the dkp data if the player was found
				SendChatMessage("You currently have "..gdkp.players[sender].dkp.." dkp.", "WHISPER", nil, sender)
				-- save that the player has been found
				PlayerFound = true
				-- the player was found, we don't have to search through the rest
				break
			end
		end
		
		-- check if the player has been found
		if PlayerFound == false then
			-- the player has no current dkp data so we tell him that he has the new player amount
			SendChatMessage("You have no current dkp data, so you have the startingamount of "..SecBid.db.global.NewPlayerDkp.." dkp.", "WHISPER", nil, sender)
		end
	end

	-- when currently bidding, check the msg for bids
	if BiddingActive and tonumber(message) then
		-- the message consists of only a number, presumably a bid
		SecBid:Print("Bidding is active and the messgae was a number")
	end
end

-- sets the time the auction is supposed to run for
function SecBid:SetTimeout(info, newValue)
	SecBid.db.global.time = newValue
	SecBid:Print("Time set to "..SecBid.db.global.time.." seconds.")
end

-- sets how many points player get that aren't in the database
function SecBid:SetNewPlayerDkp(info, newValue)
	SecBid.db.global.NewPlayerDkp = newValue
	SecBid:Print("Start value has been set to "..SecBid.db.global.NewPlayerDkp.." points.")
end

-- starts a new auction
function SecBid:StartAuction(info, newValue)
	local startingBid, itemlink = SecBid:GetArgs(newValue, 2)
	SecBid:Print("Starting auction for "..itemlink.." with a startingbid of "..startingBid..".")
	
	-- auction variables
	BiddingActive = true
	HighBidder = nil
	BiddingFinished = false
	
	-- starting the timer
	if tonumber(SecBid.db.global.time) >= 15 then
		timer = SecBid:ScheduleTimer("TimerCallback", tonumber(SecBid.db.global.time)-10)
		warning = 1
	elseif tonumber(SecBid.db.global.time) >= 7 and tonumber(SecBid.db.global.time) < 15 then
		timer = SecBid:ScheduleTimer("TimerCallback", tonumber(SecBid.db.global.time)-5)
		warning = 2
	else
		timer = SecBid:ScheduleTimer("TimerCallback", tonumber(SecBid.db.global.time))
		warning = 3
	end
end

-- this function is called once the timer runs out
function SecBid:TimerCallback()
	-- warn the raid if the timer is about to run out
	if warning == 1 then
		SecBid:Print("10 Seconds remaining.")
		timer = SecBid:ScheduleTimer("TimerCallback", 5)
		warning = 2
	elseif warning == 2 then
		SecBid:Print("5 Seconds remaining.")
		timer = SecBid:ScheduleTimer("TimerCallback", 5)
		warning = 3
	else
		-- Bidding finished, now get results and storp the bidding
		SecBid:Print("Bidding finished.")
		warning = 0
		BiddingActive = false
	end
end

